﻿using FastCloud.Base.Config;
using FastCloud.Base.Desgin;
using FastCloud.DMP.Entity;
using FastUtil.Convert;
using FastUtil.Data;
using System;
using System.Collections.Generic;
using System.Text;

namespace FastCloud.DMP.DB
{
    public class DB_CodeItem : BaseCacheML<Frame_CodeItem>
    {
        public override void Insert(Frame_CodeItem Entity)
        {
            if (string.IsNullOrEmpty(Entity.ItemGuid))
            {
                Entity.ItemGuid = GenerateUtil.GenerateUnitGuid();
            }
            Entity.AddDate = DateTime.Now;
            base.Insert(Entity);
        }

        public List<Frame_CodeItem> SelectAll()
        {
            return DTO.Select_Cache<Frame_CodeItem>(); ;
        }

        public Page SelectPage(string MainGuid,string ItemName,string ItemValue)
        {
            int PageIndex = ValueConvert.ConvertToInt32(CloudUtil.GetFormParam("PageIndex"));
            int PageSize = ValueConvert.ConvertToInt32(CloudUtil.GetFormParam("PageSize"));
            List<Frame_CodeItem> entityList = DTO.Select_Cache<Frame_CodeItem>();
            int total = entityList.Count;
            if (!string.IsNullOrEmpty(ItemName))
            {
                entityList = entityList.FindAll((Frame_CodeItem item) => item.ItemName.Contains(ItemName));
            }
            if (!string.IsNullOrEmpty(ItemValue))
            {
                entityList = entityList.FindAll((Frame_CodeItem item) => item.ItemValue.Contains(ItemValue));
            }
            if (!string.IsNullOrEmpty(MainGuid))
            {
                entityList = entityList.FindAll((Frame_CodeItem item) => item.MainGuid == MainGuid);
            }
            entityList.Sort((x, y) => -x.SortNum.CompareTo(y.SortNum));
            List<Frame_CodeItem> resultList = new List<Frame_CodeItem>();
            for (int i = (PageIndex - 1) * PageSize; i < entityList.Count && i < PageIndex * PageSize; i++)
            {
                resultList.Add(entityList[i]);
            }
            Page page = new Page();
            page.Rows = resultList;
            page.Total = total;
            return page;
        }

        public bool ExistCodeItem(string MainGuid, string ItemValue)
        {
            List<Frame_CodeItem> entityList = DTO.Select_Cache<Frame_CodeItem>();
            entityList = entityList.FindAll((Frame_CodeItem item) => item.MainGuid == MainGuid && item.ItemValue == ItemValue);
            if (entityList.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool ExistCodeItem(string MainGuid, string ItemGuid, string ItemValue)
        {
            List<Frame_CodeItem> entityList = DTO.Select_Cache<Frame_CodeItem>();
            entityList = entityList.FindAll((Frame_CodeItem item) => item.MainGuid == MainGuid && item.ItemValue == ItemValue && item.ItemGuid != ItemGuid);
            if (entityList.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public void DeleteByMainGuid(string MainGuid)
        {
            List<Frame_CodeItem> EntityList = DTO.Select_Cache<Frame_CodeItem>().FindAll(a=>a.MainGuid==MainGuid);
            foreach (Frame_CodeItem Entity in EntityList)
            {
                DeleteByID(Entity.ItemGuid);
            }

        }

        public List<Frame_CodeItem> SelectCodeItemListByMainGuid(string MainGuid)
        {
            List<Frame_CodeItem> entityList = DTO.Select_Cache<Frame_CodeItem>();
            return entityList.FindAll(a => a.MainGuid == MainGuid);
        }

    }
}
